local skel = fk.CreateSkill {
  name = "emo__pojin",
}

skel:addEffect(fk.TargetSpecified, {
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.firstTarget and data:isOnlyTarget(data.to) then
      local useParent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
      if useParent == nil then return false end
      local turnEvent = useParent:findParent(GameEvent.Turn)
      if turnEvent == nil then return false end
      local lastUseEvent = player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        return e.id < useParent.id and e.data.from == player
      end, turnEvent.id)[1]
      if lastUseEvent == nil then return false end
      local last_use = lastUseEvent.data
      if last_use.tos and #last_use.tos == 1 then
        local to = last_use.tos[1]
        if to == data.to then
          event:setCostData(self, {draw = true})
          return true
        elseif not to.dead and not to:isNude() and not player:isNude() and to ~= player then
          event:setCostData(self, {tos = {to}})
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cost_data = event:getCostData(self)
    if cost_data.draw then return true end
    local to = cost_data.tos[1]
    local cards = player.room:askToCards(player, {min_num = 1, max_num = 1, skill_name = skel.name, include_equip = true,
    prompt = "#emo__pojin-cost:"..to.id })
    if #cards > 0 then
      cost_data.cards = cards
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if cost_data.draw then
      player:drawCards(1, skel.name)
    else
      local my_cards = cost_data.cards
      local to = cost_data.tos[1]
      local to_cards = {room:askToChooseCard(player, {target = to, flag = "he", skill_name = skel.name })}
      room:swapCards(player, {{player, my_cards}, {to, to_cards}}, skel.name, Card.PlayerHand)
    end
  end,
})


Fk:loadTranslationTable{
  ["emo__pojin"] = "迫进",
  [":emo__pojin"] = "每当你使用牌指定唯一目标后，若与本回合你上一张牌的唯一目标：相同，你摸一张牌；不同，你可以用一张牌交换上次目标的一张牌。",

  ["#emo__pojin-cost"] = "迫进：你可以选择一张牌，并选择 %src 的一张牌，交换这两张牌",
}


return skel
